import {defineStore} from "pinia";
import {RouteNameEnum} from "@/utils/pm/pmRoute";

type PmMenuState = {
  projectMenu: PmMenusDTO[],
  completeMenu: PmMenusDTO[],
  menuMap: Record<string, {
    component: string,
    name: string,
  }>,
}

const BLANK_PATH = "blankRouterView/index"
export const usePmMenuStore = defineStore('PmMenu', {
  state: (): PmMenuState => ({
    projectMenu: [],
    completeMenu: [],
    menuMap: {},
  }),
  actions: {
    generateMenus(menuList: PmMenusDTO[]) {
      const projectMenu = menuList.find((menu) => menu.path === '/project');
      if (projectMenu) {
        const projectDetailMenu = projectMenu.children.find((menu) => menu.path === "ProjectDetails");
        this.completeMenu = projectDetailMenu && projectDetailMenu.children
        this.menuMap = {...staticMenu};
        this.generateMenuMap(this.completeMenu);
      }
    },
    generateMenuMap(menuList: PmMenusDTO[]) {
      menuList.forEach(menu => {
        if (menu.component && menu.componentName && menu.component !== BLANK_PATH) {
          if (menu.component.includes('.vue')) {
            this.menuMap[menu.componentName] = {
              component: menu.component.replace('.vue', ''),
              name: menu.name,
            };
          } else {
            this.menuMap[menu.componentName] = {
              component: menu.component,
              name: menu.name,
            };
          }
        }
        if (menu.children && menu.children.length > 0) {
          this.generateMenuMap(menu.children);
        }
      })
    }
  },
  persist: true,
})

const staticMenu: Record<string, { component: string, name: string }> = {
  [RouteNameEnum.WorkVisaDetail]: {
    component: 'project/projectDetails/deliveryPhase/implDelivery/engineering/workVisa/detail/index',
    name: '工作签证详情',
  }
}
